package com.dongdongshop.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSON;
import com.dongdongshop.pojo.*;
import com.dongdongshop.result.Result;
import com.dongdongshop.result.ResultEnum;
import com.dongdongshop.service.GoodsService;
import com.dongdongshop.service.ItemCatService;
import com.dongdongshop.service.SpecificationService;
import com.dongdongshop.service.TypeTemplateService;
import com.dongdongshop.util.PageUtil;
import com.dongdongshop.vo.SpecVo;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.SecurityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("goods")
public class GoodsController {

    private static final Logger logger = LoggerFactory.getLogger(GoodsController.class);

    @Reference
    private GoodsService goodsService;

    @Reference
    private ItemCatService itemCatService;

    @Reference
    private TypeTemplateService typeTemplateService;

    @Reference
    private SpecificationService specificationService;

    @RequestMapping("toGoodsEdit")
    public String toGoodsEdit(){
        return "admin/goods_edit";
    }

    @RequestMapping("toGoodsShow")
    public String toGoodsShow(){
        return "admin/goods";
    }

    @RequestMapping("getItemCatSort")
    @ResponseBody
    public Result getItemCatSort(@RequestParam(defaultValue = "0") Long parentId){
        try {
            List<TbItemCat> itemCats = itemCatService.getItemByParentId(parentId);
            if (itemCats == null){
                return Result.jsonResponse(ResultEnum.BAD_GATEWAY);
            }
            return Result.jsonResponse(ResultEnum.OK).setData(itemCats);
        }catch (Exception e){
            logger.error("系统报错",e);
            return Result.jsonResponse(ResultEnum.INTERNAL_SERVER_ERROR);
        }
    }

    @RequestMapping("getTemplateByItemCatId")
    @ResponseBody
    public Result getTemplateByItemCatId(Long id){

        try {
            TbTypeTemplate tbTypeTemplate = typeTemplateService.selectTemplateById(id);
            if (tbTypeTemplate == null){
                return Result.jsonResponse(ResultEnum.BAD_GATEWAY);
            }
            return Result.jsonResponse(ResultEnum.OK).setData(tbTypeTemplate);
        }catch (Exception e){
            logger.error("系统报错",e);
            return Result.jsonResponse(ResultEnum.INTERNAL_SERVER_ERROR);
        }
    }

    @RequestMapping("insertGoods")
    @ResponseBody
    public Result insertGoods(TbGoods tbGoods, TbGoodsDesc tbGoodsDesc,String itemStr,@RequestParam("parentId") Long parentId){
        try {
            TbTypeTemplate tbTypeTemplate = typeTemplateService.selectTemplateById(parentId);
            TbSeller seller = (TbSeller) SecurityUtils.getSubject().getPrincipal();
            tbGoods.setSellerId(seller.getSellerId());
            tbGoods.setIsMarketable("1");
            tbGoods.setIsDelete("0");
            tbGoods.setAuditStatus("0");
            tbGoods.setTypeTemplateId(tbTypeTemplate.getId());
            tbGoods.setIsEnableSpec("1");
            List<TbItem> tbItems = JSON.parseArray(itemStr,TbItem.class);
            int i = goodsService.insertGoods(tbGoods,tbGoodsDesc,tbItems,seller.getName(),parentId);
            if (i <= 0){
                return Result.jsonResponse(ResultEnum.BAD_GATEWAY);
            }
            return Result.jsonResponse(ResultEnum.OK);
        }catch (Exception e){
            logger.error("系统报错",e);
            return Result.jsonResponse(ResultEnum.INTERNAL_SERVER_ERROR);
        }
    }

    @RequestMapping("getSpecsAndOptionsByItemCatId")
    @ResponseBody
    public Result getSpecsAndOptionsByItemCatId(Long itemCatId){
        try {
            //查看itemCat数据
            TbItemCat itemById = itemCatService.getItemById(itemCatId);
            //根据获取的typeId查询typeTemplate数据
            TbTypeTemplate templateById = typeTemplateService.getTemplateById(itemById.getTypeId());
            //把查出来的SpecIds转成list
            List<SpecVo> specVoList = JSON.parseArray(templateById.getSpecIds(),SpecVo.class);
            for (SpecVo specVo : specVoList){
                //根据specVo中的id查询商品具体规格
                List<TbSpecificationOption> tbSpecificationOptionsById = specificationService.getTbSpecificationOptionsById(specVo.getId());
                specVo.setOptions(tbSpecificationOptionsById);
            }
            return Result.jsonResponse(ResultEnum.OK).setData(specVoList);
        }catch (Exception e){
            logger.error("系统报错",e);
            return Result.jsonResponse(ResultEnum.INTERNAL_SERVER_ERROR);
        }
    }

    @RequestMapping("getListGoods")
    @ResponseBody
    public Result<PageUtil<TbGoods>> getListGoods(String goodsName, String auditStatus,@RequestParam(value = "pageNumber", defaultValue = "1") Integer pageNumber,
                                                  @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize){
        try {
            PageInfo<TbGoods> pageInfo = goodsService.getListGoods(goodsName,auditStatus,pageNumber,pageSize);
            PageUtil<TbGoods> pageUtil = new PageUtil<>();
            pageUtil.setPageNumber(pageNumber);
            pageUtil.setPageSize(pageSize);
            pageUtil.setList(pageInfo.getList());
            pageUtil.setTotal(pageInfo.getTotal());
            return Result.jsonResponse(ResultEnum.OK).setData(pageUtil);
        }catch (Exception e){
            logger.error("系统报错",e);
            return Result.jsonResponse(ResultEnum.INTERNAL_SERVER_ERROR);
        }
    }

    @RequestMapping("updateGoodsAuditStatus")
    @ResponseBody
    public Result updateGoodsAuditStatus(@RequestParam("ids[]") Long[] ids,@RequestParam("auditStatus") String auditStatus){
        try {
            int i = goodsService.updateGoodsAuditStatus(ids,auditStatus);
            return Result.jsonResponse(ResultEnum.OK);
        }catch (Exception e){
            logger.error("系统报错",e);
            return Result.jsonResponse(ResultEnum.INTERNAL_SERVER_ERROR);
        }
    }


}
